/*
 * naviX11.h - (X11Lib include)EmbedContainer for Navit
 *
 * Copyright (C) 2010-2011 Serge Begger
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


#ifndef NAVIX11_H
#define NAVIX11_H

#include <X11/Xlib.h>
#include <X11/Xutil.h>

Window windowWithName(Display *dpy, Window top ,char *nameWin)
{
    Window *children, dummy;
    unsigned int nchildren, i;
    Window w=0;
    char *window_name;
    if (XFetchName(dpy, top, &window_name) && !strcmp(window_name, nameWin))
        return(top);
    if (!XQueryTree(dpy, top, &dummy, &dummy, &children, &nchildren))
        return(0);
    for (i=0; i<nchildren; i++) {
        w = windowWithName(dpy, children[i], nameWin);
        if (w)
            break;
   }
   if (children) XFree ((char *)children);
   return(w);
}

long getWinID(QString winName)
{
    Display *dsp = XOpenDisplay(0);
    int scrn = DefaultScreen(dsp);
    Window root = RootWindow(dsp, scrn);
    Window navit = windowWithName(dsp,root,winName.toLocal8Bit().data());
    return navit;
}

#endif // NAVIX11_H
